package com.wumai.modernguilib.component;

import com.mojang.blaze3d.matrix.MatrixStack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.AbstractGui;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;

import java.util.function.Consumer;

public class TextField extends TextComponent {
    protected String placeholder = "";
    protected int placeholderColor = 0xFFA0A0A0; // 默认灰色占位符
    protected int borderColorFocused = 0xFF00FF00; // 默认绿色聚焦边框
    protected boolean isFocused = false;
    protected int cursorPosition = 0;
    protected int selectionStart = 0;
    protected boolean drawCursor = true;
    protected int tickCount = 0;
    
    // 回调事件
    protected Consumer<String> onTextChange; // 文本改变时的回调
    protected Runnable onFocusLost; // 失去焦点时的回调

    public TextField() {
        // 新的构造函数，不指定位置和大小，将由父容器或自动布局系统确定
        super("");
        this.text = new StringTextComponent("");
        this.textColor = 0xFFFFFFFF;
        this.backgroundColor = 0x80000000;
        this.borderColor = 0xFFFFFFFF;
    }

    public TextField(int x, int y, int width, int height) {
        super(x, y, width, height, "");
        this.text = new StringTextComponent("");
        this.textColor = 0xFFFFFFFF;
        this.backgroundColor = 0x80000000;
        this.borderColor = 0xFFFFFFFF;
    }


    @Override
    protected void renderContent(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
        tickCount++;

        // 确定当前使用的边框颜色
        int currentBorderColor = isFocused ? borderColorFocused : borderColor;
        this.setBorderColor(currentBorderColor);

        // 绘制文字
        FontRenderer fontRenderer = Minecraft.getInstance().font;
        int textHeight = fontRenderer.lineHeight;
        
        // 显示占位符或文本
        String displayText = text.getString().isEmpty() ? placeholder : text.getString();
        int color = text.getString().isEmpty() ? placeholderColor : textColor;
        
        // 简化处理，实际项目中可能需要处理滚动和光标位置
        fontRenderer.drawShadow(matrixStack, displayText, mouseX, mouseY, color);
        
        // 绘制光标（如果聚焦）
        if (isFocused && tickCount % 40 < 20) { // 光标闪烁
            int cursorX = mouseX + fontRenderer.width(text.getString().substring(0, Math.min(cursorPosition, text.getString().length())));
            AbstractGui.fill(matrixStack, cursorX, mouseY, cursorX + 1, mouseY + textHeight, textColor);
        }
    }

    @Override
    protected void renderBackground(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {

    }

    // 设置文本
    public void setText(String text) {
        super.setText(text);
        cursorPosition = text.length();
        selectionStart = cursorPosition;
        
        // 触发文本改变回调
        if (onTextChange != null) {
            onTextChange.accept(text);
        }
    }

    // 获取文本
    public ITextComponent getText() {
        return super.getText();
    }

    // 设置占位符
    public void setPlaceholder(String placeholder) {
        this.placeholder = placeholder;
    }

    // 获取占位符
    public String getPlaceholder() {
        return placeholder;
    }

    // 设置文字颜色
    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }

    // 设置占位符颜色
    public void setPlaceholderColor(int placeholderColor) {
        this.placeholderColor = placeholderColor;
    }

    // 设置背景颜色
    public void setBackgroundColor(int backgroundColor) {
        this.backgroundColor = backgroundColor;
    }

    // 设置边框颜色
    public void setBorderColor(int borderColor) {
        this.borderColor = borderColor;
    }

    // 设置聚焦时边框颜色
    public void setBorderColorFocused(int borderColorFocused) {
        this.borderColorFocused = borderColorFocused;
    }

    // 设置文本改变回调
    public void setOnTextChange(Consumer<String> onTextChange) {
        this.onTextChange = onTextChange;
    }

    // 设置失去焦点回调
    public void setOnFocusLost(Runnable onFocusLost) {
        this.onFocusLost = onFocusLost;
    }

    // 鼠标点击事件
    public boolean mouseClicked(double mouseX, double mouseY, int button) {
        if (isMouseOver(mouseX, mouseY)) {
            isFocused = true;
            return true;
        } else if (isFocused) {
            isFocused = false;
            // 触发失去焦点回调
            if (onFocusLost != null) {
                onFocusLost.run();
            }
            return true;
        }
        return false;
    }

    // 键盘输入事件
    public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
        if (!isFocused) return false;
        
        // 这里应该处理具体的键盘事件，比如删除、输入字符等
        // 为简化起见，我们只处理失去焦点的情况
        if (keyCode == 256) { // ESC键
            isFocused = false;
            if (onFocusLost != null) {
                onFocusLost.run();
            }
            return true;
        }
        
        return false;
    }

    // 字符输入事件
    public boolean charTyped(char codePoint, int modifiers) {
        if (!isFocused) return false;
        
        // 添加字符到文本
        setText(this.text.getString() + codePoint);
        cursorPosition = this.text.getString().length();
        selectionStart = cursorPosition;
        
        // 触发文本改变回调
        if (onTextChange != null) {
            onTextChange.accept(this.text.getString());
        }
        
        return true;
    }
}